/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Binhluan;
import pojo.Danhsachsanpham;
import pojo.Donhang;
import pojo.Loaisanpham;
import pojo.Nguoidung;
import pojo.Sanpham;
import until.HibernateUtil;

/**
 *
 * @author Alvis
 */
public class DonHangDao {
    private Session session;

    public DonHangDao() {
        session = HibernateUtil.getSessionFactory().openSession();

    }
     public void Insert(Donhang value) {
        try {
            session.getTransaction().begin();
            // nguoidung.setManguoidung(null);


            String sql1 = "select nextval('donhang_madonhang_seq')";
            Statement st = session.connection().createStatement();
            ResultSet rq = (ResultSet) st.executeQuery(sql1);
            int ma = 0;
            while (rq.next()) {
                ma = rq.getInt("nextval");
            }
            value.setMadonhang(ma);
            session.save(value);
            session.flush();
            session.getTransaction().commit();
        } catch (SQLException ex) {
            Logger.getLogger(Donhang.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
     public void Update(Donhang value, int id) {
        session.getTransaction().begin();
        Donhang valueTemp = (Donhang) session.get(Donhang.class, id);
        valueTemp.setThanhvien(value.getThanhvien());
        valueTemp.setDiachi(value.getDiachi());
        valueTemp.setSodienthoainguoinhan(value.getSodienthoainguoinhan());
        valueTemp.setTennguoinhan(value.getTennguoinhan());
        valueTemp.setTongtien(value.getTongtien());
        session.update(valueTemp);
         session.flush();
        session.getTransaction().commit();
    }
      public void Delete(int id) {
        session.getTransaction().begin();
        String sql = "from Danhsachsanpham where madonhang=?";
        Query query = session.createQuery(sql);
        query.setInteger(0,id);
        List result = query.list();
        Danhsachsanpham[] listDanhSachSanPham = new Danhsachsanpham[result.size()];
        result.toArray(listDanhSachSanPham);
        for(int i=0;i<listDanhSachSanPham.length;i++)
        {
           
            session.delete(session.get(Danhsachsanpham.class,listDanhSachSanPham[i].getId()));
            
            
        }
        session.delete(session.get(Donhang.class, id));
        session.flush();
        session.getTransaction().commit();
        
      }
      public Donhang[] getAllOrder() {
        session.getTransaction().begin();
        String sql = "from Donhang";
        Query query = session.createQuery(sql);
        List result = query.list();
        Donhang[] listvalue = new Donhang[result.size()];
        result.toArray(listvalue);
        return listvalue;
    }
    public Donhang getOrder(int id) {

        session.getTransaction().begin();
        String sql = "from Donhang where madonhang=?";
        Query query = session.createQuery(sql);

        query.setInteger(0,id);
        Donhang result = (Donhang) query.uniqueResult();
        session.flush();
        session.getTransaction().commit();
        return result;

    }
}
